package cn.cmvideo.ps;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application
{
	static final String TOPICEXCHANGENAME = "spring-boot-exchange";
	private static final String QUEUENAME = "spring-boot";

	// declare the queue
	@Bean
	Queue queue()
	{
		return new Queue(QUEUENAME);
	}

	// declare the exchange
	@Bean
	TopicExchange exchange()
	{
		return new TopicExchange(TOPICEXCHANGENAME);
	}
	
	// bind queue exchange and routingKey
	@Bean
	Binding binding(Queue queue,TopicExchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
	}
	
	// message listener container
	@Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(QUEUENAME);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    
    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args).close();
    }
}
